package com.xiongwei.thread;

import java.time.LocalTime;

/**
 * 守护线程是为其他线程服务的线程
 *
 * 所有守护线程都执行完毕后,虚拟机退出
 *
 * 守护线程不能持有需要关闭的资源(如打开文件等)
 *
 * @author XiongWei
 * @date 2021/1/13
 */
public class DeamonThread {
    public static void main(String[] args) throws InterruptedException {
        Thread t = new TimerThread();
        t.setDaemon(true);
        t.start();
        Thread.sleep(100);
        System.out.println("main end");
    }
}

class TimerThread extends Thread {
    @Override
    public void run() {
        while (true) {
            System.out.println(LocalTime.now());
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
